﻿using System;
using fastCSharp;

namespace diantou.web.contest
{
    /// <summary>
    /// 问题题解
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPool = true, IsPage = false)]
    partial class problemSolution : problemView<problemSolution>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problemId"></param>
        /// <returns></returns>
        protected override bool loadView(int problemId)
        {
            if (currentUser != null)
            {
                if (problemId == 0)
                {
                    checkSearchEngine(diantou.dataModel.path.pub.Default.ContestListPath, webConfig.NotFound404);
                    return false;
                }
                if ((problem = diantou.dataAccess.clientCache.Problem[problemId]) != null)
                {
                    if (problem.Solution.Length != 0)
                    {
                        int point = diantou.dataAccess.clientCache.ContestProblem[new diantou.dataModel.contestProblem.primaryKey { ContestId = problem.ContestId, ProblemId = problem.Id }];
                        if (point >= 0)
                        {
                            contest = diantou.dataAccess.clientCache.Contest[problem.ContestId];
                            if (contest != null)
                            {
                                bool isClone = false;
                                if (problem.BasePoint != point)
                                {
                                    isClone = true;
                                    (problem = problem.Clone()).BasePoint = point;
                                }
                                if (currentUser.IsOnlineJudgeEditor(problem) || problem.TestUserId == currentUser.Id) return true;
                                if (date.Now >= contest.EndTime)
                                {
                                    if (!userProblem.ContestIsAccepted && !userProblem.IsSolution)
                                    {
                                        if (!isClone) problem = problem.Clone();
                                        problem.Solution = string.Empty;
                                    }
                                    return true;
                                }
                            }
                        }
                    }
                    problem = null;
                }
            }
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
    }
}
